Ext.onReady(function() {
   var store = Ext.create('Ext.data.TreeStore', {
       proxy: {
           type: 'ajax',
           url: '/menu_list/'
       }
    });

	var menuTree=Ext.create('Ext.tree.Panel',{
		title: '菜单',
		store: store,
		rootVisible : false, 
		//useArrows: true,
		autoScroll : true, 
		width:340,
		split:true,
		region:'west',  
		listeners : {
			'itemclick' : function(view, record, item, index, e) {
				menuForm.getForm().setValues({
					menu_id:record.get('id'),
					menu_name:record.get('text'),
					menu_url:record.raw.url,
					menu_display:record.raw.display,
					menu_leaf:record.raw.leaf,
					menu_navi:record.raw.navi,
					menu_parent_id:record.raw.parent_id
				});
			},
			scope : this
		}
	});
	var menuForm=Ext.create('Ext.form.Panel',{
		title:'操作',
		bodyPadding:10,
		region:'center',
		defaults:{
			anchor:'100%',
			labelWidth:100,
			msgTarget: 'side'
		},        
		dockedItems: [{
            xtype: 'toolbar',
            dock: 'top',
            items: [{
                text: '添加菜单',
                iconCls:'add',
                handler: function(){
                	menuForm.getForm().reset();
                }
            }, {
                text: '删除菜单',
                iconCls:'delete',
                handler: function(){
                	menu_id=menuForm.getForm().findField('menu_id').getValue();
                	if(menu_id!=''){
                		Ext.MessageBox.confirm('提示', '确认删除吗?', function(btn){
                			if(btn=='yes'){
								menuForm.getForm().submit({
									waitTitle : '请稍候',
									waitMsg : '正在处理......',
									url : '/menu_del/',
									success : function(form, action) {
										Ext.Msg.alert('系统提示', action.result.message);
										menuForm.getForm().reset();
										menuTree.getStore().load();
									},
									failure : function(form, action) {
										Ext.Msg.alert('系统提示', action.result.message);
									}
								});                 				
                			}
                		});
                	}else{
                		Ext.Msg.alert('系统提示', '请选择需要删除的菜单!');
                	}
                }
            }, {
                text: '确认保存',
                iconCls:'save',
                handler: function(){
    				if (menuForm.getForm().isValid()) {
    					menuForm.getForm().submit({
    						waitTitle : '请稍候',
    						waitMsg : '正在处理......',
    						url : '/menu_save/',
    						success : function(form, action) {
    							//Ext.Msg.alert('系统提示', action.result.message);
    							menuForm.getForm().reset();
    							menuTree.getStore().load();
    						},
    						failure : function(form, action) {
    							Ext.Msg.alert('系统提示', action.result.message);
    							menuForm.getForm().reset();
    						}
    					});
    				}
                }
            }]
        }],
		items :[{
            xtype: 'hiddenfield',
            fieldLabel: 'id',
            name:'menu_id'
        },{
			xtype:'textfield',
			name:'menu_name',
			fieldLabel:'菜单名称',
			allowBlank: false
		},{
			xtype:'textfield',
			name:'menu_url',
			fieldLabel:'菜单地址',
			allowBlank: false			
		}, {
            xtype: 'fieldset',
            height: 45,
            padding: 5,
            layout: {
                align: 'stretch',
                type: 'hbox'
            },
            title: '菜单操作',
            items: [
                {
                    xtype: 'checkboxfield',
                    name:'menu_display',
                    inputValue:'1',
                    boxLabel: '是否显示&nbsp;&nbsp;&nbsp;&nbsp;',
                    anchor: '100%'
                },
                {
                    xtype: 'checkboxfield',
                    name:'menu_leaf',
                    inputValue:'1',
                    boxLabel: '是否子节点&nbsp;&nbsp;&nbsp;&nbsp;',
                    anchor: '100%'
                },
                {
                    xtype: 'checkboxfield',
                    name:'menu_navi',
                    inputValue:'1',
                    boxLabel: '是否导航&nbsp;&nbsp;&nbsp;&nbsp;',
                    anchor: '100%'
                }
            ]
        },{
            xtype: 'combobox',
            name:'menu_parent_id',
            fieldLabel: '上一级菜单', 
            displayField:'text',
            valueField: 'id',
            store: new Ext.data.Store({ 
                singleton : true, 
        	    proxy: {
        	        type: 'ajax',
        	        url: '/menu_select/',
        	        actionMethods:'post',
        	        reader: {
        	            type: 'json'
        	        }
        	    },
        	    fields:['id', 'text'],
                autoLoad:true 
            }), 
            loadingText:'正在加载数据，请稍侯……', 
            typeAhead: true
		}]
	});
	
	var menuPanel=Ext.create('Ext.container.Viewport', {
		renderTo: 'menu-panel',
		layout:'border',
		items:[menuTree,menuForm]
	});		
});